package seqlist.queue.impl;


import seqlist.queue.Queue;

import java.util.NoSuchElementException;

class Node<E> {
    E val;
    Node next;

    public Node(E val) {
        this.val = val;
    }
}
public  class Linkedqueque<E> implements Queue<E> {
    private int size;
    private Node<E> head;
    private Node<E> tail;

    @Override
    public void offer(E val) {
       Node<E> node = new Node<>(val);
       if (head == null){
           head = tail = node;
       }else {
           tail.next = node;
           tail=node;
       }
       size++;
    }

    @Override
    public E poll() {
        if (isEmpty()) {
            throw new NoSuchElementException("queue is empty!cannot poll!");
    }
        E val = head.val;
        Node<E> node=head;
        head=head.next;
        node.next=node=null;
        size--;
        return val;

    }

    @Override
    public E peek() {
        if (isEmpty()){
            throw new NoSuchElementException("queue is empty!cannot peek!");
        }
        return head.val;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public String toString() {
        StringBuilder sb=new StringBuilder();
        sb.append("front[");
        for (Node x=head;x!=null;x=x.next){
            sb.append(x.val);
            if (x.next!=null){
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }
}
